﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO.Ports;
using System.Windows.Forms;
using KEW.InterFace;

namespace AutoGam
{
    public class Device
    {
        public static ICard ComCardDevice;
        public static ICard UsbCardDevice;
        public static ICard MHCardDevice;
        public static Ds.Printer.DsPrinter Printer;
        private static SerialPort _port;

        public static String GetCardAndReturnSerial()
        {
            int count = 0;
            String serial = UsbCardDevice.GetSerial();
            if (serial == String.Empty)
            {
                GetCard();
                while (count < 15)
                {
                    serial = UsbCardDevice.GetSerial();
                    if (serial != String.Empty)
                    {
                        break;
                    }
                    count++;
                    System.Windows.Forms.Application.DoEvents();
                    Thread.Sleep(200);
                    //Device.UsbCardDevice.ReSet();
                }
            }
            return serial;
        }
        
        public static void GetCard()
        {
            byte[] buffer = new byte[] { 0x02, 0x44, 0x48, 0x03, 0x0D, 0x05};
            if (_port != null)
            {
                _port.Write(buffer, 0, buffer.Length);
            }
        }

        public static void SendCard()
        {
            byte[] buffer = new byte[] { 0x02, 0x45, 0x53, 0x03, 0x17, 0x05 };
            if (_port != null)
                _port.Write(buffer, 0, buffer.Length);
        }

        public static void ReviceCard()
        {
            byte[] buffer = new byte[] { 0x02, 0x43, 0x50, 0x03, 0x12, 0x05 };
            if (_port != null)
                _port.Write(buffer, 0, buffer.Length);
        }


        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public static Boolean Init()
        {
            try
            {
                MHCardDevice = new UsbCardDevice();
                UsbCardDevice = new UsbLaiKa();
                //Printer = new Ds.Printer.DsPrinter(System.Environment.CurrentDirectory + "\\模板\\");
                if (!Program.IsDebug)
                {
                    ComCardDevice = new ComCardDevice();
                    _port = new System.IO.Ports.SerialPort("COM1");
                    _port.BaudRate = 9600;
                    _port.Parity = Parity.None;
                    _port.StopBits = StopBits.One;
                    _port.DataBits = 8;
                    _port.Handshake = Handshake.None;
                    _port.ReceivedBytesThreshold = 4;//设置 DataReceived 事件发生前内部输入缓冲区中的字节数
                    _port.Open();
                }
                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
        }

        
        /// <summary>
        /// 退出
        /// </summary>
        public static void Exit()
        {
            if (ComCardDevice != null)
            ComCardDevice.Exit();
            if (UsbCardDevice != null)
            UsbCardDevice.Exit();
        }

        public static void ButtonVoice()
        {
            //_sound.Stop();
            _sound.SoundLocation = System.Environment.CurrentDirectory + "//Button.wav";
            _sound.Play();
            CountDown = 50;
        }

        public static void Voice(String fileName)
        {
            _sound.Stop();
            _sound.SoundLocation = System.Environment.CurrentDirectory + "//" + fileName;
            _sound.Play();
        }

        private static System.Media.SoundPlayer _music = new System.Media.SoundPlayer();
        private static System.Media.SoundPlayer _sound = new System.Media.SoundPlayer();
        private static DateTime _buttonTime = DateTime.Now;
        public static int CountDown = -1;
            
    }
}
